<?php
namespace app\admin\controller;
use app\common\model\RechargeRecord as RechargeRecordModel;
use app\common\controller\AdminBase;
use think\Db;
/**
 * 管理
 * Class Adminrechargerecord
 * @package app\admin\controller
 */
class RechargeRecord extends AdminBase
{
    protected $rechargerecord_model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->rechargerecord_model = new RechargeRecordModel();

    }
    /**
     * 管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function index($user_name = '',$nav_name = '',$status = '2',$coding = '',$page = 1)
    {
        $map = [];
        if($user_name){
            $map['user_name'] = ['like',"%{$user_name}%"];
        }
        if($nav_name){
            $map['nav_name'] = ['like',"%{$nav_name}%"];
        }
        if($status != 2){
            $map['status'] = $status;
        }
        if($coding){
            $map['coding'] = ['like',"%{$coding}%"];
        }
        $rechargerecord_list = $this->rechargerecord_model->where($map)->order('id DESC')->paginate(15, false, ['page' => $page]);
        return $this->fetch('index', ['rechargerecord_list' => $rechargerecord_list,'user_name' => $user_name,'nav_name' => $nav_name,'status' => $status,'coding' => $coding]);
    }

    /**
     * 更新
     * @param $id
     */
    public function update($id)
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $validate_result = $this->validate($data, 'RechargeRecord');
            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                if(intval($data['low_price']) >= intval($data['price'])){
                    $this->error('降价必须小于原价');
                }
                if ($this->rechargerecord_model->allowField(true)->save($data, $id) !== false) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            }
        }
    }
    /**
     * 删除
     * @param $id
     */
    public function delete($id = '', $ids = [])
    {
        $id = $ids ? array_merge($ids) : $id;
        if ($id) {
            if ($this->rechargerecord_model->destroy($id)) {
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        } else {
            $this->error('请选择需要删除的内容');
        }
    }

    /**
     * 文章审核状态切换
     * @param array  $ids
     * @param string $type 操作类型
     */
    public function toggle($ids = [], $type = '' ,$id = '')
    {
        $data   = [];
        $status = $type == 'audit' ? 1 : 0;
        if(!empty($id)){
            if($this->rechargerecord_model->save(['status'=>$status],['id'=>$id])){
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
        if (!empty($ids)) {
            foreach ($ids as $value) {
                $data[] = ['id' => $value, 'status' => $status];
            }
            if ($this->rechargerecord_model->saveAll($data)) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        } else {
            $this->error('请选择需要操作的文章');
        }
    }

    /**
     * 查看
     */
    public function reading($id)
    {
        $rechargerecord = $this->rechargerecord_model->find($id);
        return $this->fetch('reading', ['rechargerecord' => $rechargerecord]);
    }
}